package com.source.lightfowl.person.controller;

import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.person.service.PersonService;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "用户注册与登录")
@RestController
@CrossOrigin
public class PersonController {

    @Autowired
    private PersonService personService;

    @ApiOperation(value = "注册")
    @PostMapping(value = "/person/register", produces = MediaType.APPLICATION_JSON_VALUE)
    public MyResult register(String username, String password, String email, HttpServletRequest request, String clientCode) {
        System.out.println(username+password+email+clientCode);
        if(username!=null&&password!=null&&username!=""&&password!=""&&email!=null&&email!=""){
            return personService.register(username,password,email,request,clientCode);
        }else {
            return MyResult.build(1,"用户名或密码或邮箱为空");
        }
    }

    @ApiOperation(value = "注册2包含上传文件")
    @PostMapping(value = "/person/register2", produces = MediaType.APPLICATION_JSON_VALUE)
    public MyResult register2(Person person, HttpServletRequest request, String clientCode, MultipartFile file) {
        if(person.getLoginName()!=null&&person.getPassword()!=null&&person.getLoginName()!=""&&person.getPassword().toString()!=""&&person.getEmail()!=null&&person.getEmail()!=""){
            return personService.register(person,request,clientCode, file);
        }else {
            return MyResult.build(1,"用户名或密码或邮箱为空");
        }
    }

    @ApiOperation(value = "修改密码")
    @PostMapping(value = "/person/password/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @LogAnnotation(operEvent = "操作类型：用户修改密码",operType = 2)
    public MyResult updatePassword(String password, String email, HttpServletRequest request, String clientCode) {
        if(password!=""&&email!=null&&email!=""){
            return personService.updatePassword(password,email,request,clientCode);
        }else {
            return MyResult.build(1,"用户名或密码或邮箱为空");
        }
    }

    @ApiOperation(value = "登录")
    @PostMapping(value = "/person/login", produces = MediaType.APPLICATION_JSON_VALUE)
    @LogAnnotation(operEvent = "操作类型：用户登陆",operType = 1)
    public MyResult login(String username, String password, HttpServletRequest request) {
        if(username!=null&&password!=null&&username!=""&&password!=""){
            return personService.login(username,password,request);
        }else {
            return MyResult.build(1,"用户名或密码错误");
        }
    }

    @ApiOperation(value = "退出登录(弃)")
    @PostMapping(value = "/person/logout", produces = MediaType.APPLICATION_JSON_VALUE)
    @LogAnnotation(operEvent = "操作类型：用户退出登录",operType = 1)
    public MyResult logout(HttpServletRequest request) {
        return personService.logout(request);
    }

    @ApiOperation(value = "发送邮箱验证码")
    @PostMapping(value = "/person/send/mail", produces = MediaType.APPLICATION_JSON_VALUE)
    public MyResult sendMail(String email, HttpServletRequest request) {
        if (email!=null&&email!=""){
            return personService.sendMail(email,request);
        }else {
            return MyResult.build(1,"邮箱不能为空");
        }
    }

    @ApiOperation(value = "检查用户名是否重复")
    @PostMapping(value = "/person/check/username", produces = MediaType.APPLICATION_JSON_VALUE)
    public MyResult checkUsername(String username) {
        if (username!=null&&username!=""){
            return personService.checkUsername(username);
        }else {
            return MyResult.build(1,"用户名不能为空");
        }
    }

    @ApiOperation(value = "检查邮箱是否重复")
    @PostMapping(value = "/person/check/mail", produces = MediaType.APPLICATION_JSON_VALUE)
    public MyResult sendMail(String email) {
        if (email!=null&&email!=""){
            return personService.checkEmail(email);
        }else {
            return MyResult.build(1,"邮箱不能为空");
        }
    }


    /*
    @PostMapping("/adminRegister")
    public MyResult adminRegister(String username, String password, String email, HttpServletRequest request){
        String clientCode = "true";
        return personService.register(username,password,email,request,clientCode);
    }*/


}
